def GeometricAvgReturn(dataseries, nYear, lag=0):
    count = dataseries.Count()
    if count - lag < nYear:
        return None
    #
    pi = 1
    for i in range(nYear):
        ret = dataseries.GetByIndex(count - 1- lag - i)
        pi = pi * (1 + ret)
    #
    ret = np.power(pi, 1.0/nYear) - 1
    return ret


def CalcAverageProfitabilityFactor(database, instruments, datetime1, datetime2):

    fundamentalsBySymbol = {}
    #
    count = 0
    for instrument in instruments:
        symbol = instrument["Symbol"]
        count = count + 1
        #
        dataSeries_AveROA_3Yr = DataSeries.DataSeries(symbol + "_AvgROA3Yr_Factor")
        dataSeries_AveROE_3Yr = DataSeries.DataSeries(symbol + "_AvgROE3Yr_Factor")

        ds_roa = database.getDataSeries(symbol + "_ROALYR_Factor")
        ds_roa = database.getDataSeries(symbol + "_ROALYR_Factor")


        count = ds_roa.Count()
        for i in range(3):
            ds_roa.GetByIndex(count-1-i)


def MarginStability(database, instruments, datetime1, datetime2):
    pass


def FranchisePower(database, instruments, datetime1, datetime2):

    #Percentile(Average(
    # Percentile(8yr_ROA),
    # Percentile(8yr_ROC),
    # Percentile(FCFA),
    # Max( Percentile(MG), Percentile(MS) ) ) )

    # FCFA = LT Free Cashflow on Asset
    pass
